import httpUtil from "@/utils/httpUtil";
import deviceUtil from "../utils/deviceUtil";


const login = (mobile, captcha, callBack) => {
	let a = httpUtil.getA()
	let user_id = getUserId()
	let type = 1; //类型:1=支付宝小程序,2=微信h5,3=微信小程序,4=支付宝h5，默认1
	// #ifdef H5
	if (deviceUtil.isAliBrowser()) {
		type = 4
	}
	if (deviceUtil.isWeChatBrowser()) {
		type = 2
	}
	// #endif
	// #ifdef MP-ALIPAY
	type = 1;
	// #endif
	// #ifdef MP-WEIXIN
	type = 3
	// #endif
	let params = {
		a,
		mobile,
		captcha,
		user_id,
		type,
		event: 'pay',
	}
	httpUtil.post('/api/rf/login', params, res => {
		setToken(res.data.token)
		setUserInfo(res.data.user)
		if (callBack) {
			callBack(res)
		}
	})
}

const setToken = (token) => {
	uni.setStorageSync('token', token)
}

const getToken = () => {
	return uni.getStorageSync('token')
}

const setUserInfo = (userInfo) => {
	uni.setStorageSync('userInfo', JSON.stringify(userInfo))
}

const getUserInfo = () => {
	let userInfo = uni.getStorageSync('userInfo')
	if (userInfo) {
		return JSON.parse(userInfo)
	}
	return false
}

const initUserInfo = (callBack) => {
	let token = getToken()
	let params = {
		token
	}
	httpUtil.post('/api/rf/getUserInfo', params, res => {
		setUserInfo(res.data.user)
		if (callBack) {
			callBack(res)
		}
	})
}

/**
 *
 * @param type 爱奇艺，腾讯视频，芒果TV，优酷会员，南瓜视频
 * @param img
 * @param callBack
 */
const receive = (type, img, callBack) => {
	let params = {
		type,
		img
	}
	httpUtil.post('/api/rf/lqvip', params, res => {
		if (callBack) {
			callBack(res)
		}
	})
}

/**
 * 领取记录列表
 * @param callBack
 */
const receiveRecordList = (callBack) => {
	httpUtil.post('/api/rf/lqlog', {}, res => {
		if (callBack) {
			callBack(res)
		}
	})
}

const setUserId = (userId) => {
	uni.setStorageSync('userId', userId)
}

const getUserId = () => {
	return uni.getStorageSync('userId')
}

const setMobile = (value) => {
	uni.setStorageSync('mobile', value)
}
const getMobile = () => {
	return uni.getStorageSync('mobile');
}
const removeMobile = () => {
	uni.removeStorageSync('mobile')
}

const initUserId = (code) => {
	// 类型:1=支付宝小程序,2=微信h5,3=微信小程序,4=支付宝h5，默认1
	let type = 0;
	// #ifdef H5
	if (deviceUtil.isWeChatBrowser()) {
		type = 2
	}
	if (deviceUtil.isAliBrowser()) {
		type = 4
	}
	// #endif
	// #ifdef MP-ALIPAY
	type = 1
	// #endif
	// #ifdef MP-WEIXIN
	type = 3
	// #endif
	let params = {
		code,
		type
	}
	httpUtil.post('/api/rf/getAliUseridHjk', params, res => {
		console.log('initUserId', res)
		setUserId(res.data.user_id)
	})
}
const getMiniLoginCode = (callBack) => {
	uni.getProvider({
		service: 'payment',
		success: res => {
			console.log('getProvider success', res);
			console.log(res.provider[0]);
			uni.login({
				provider: res.provider[0],
				success: loginRes => {
					// console.log("loginRes.code", loginRes.code);
					// getUserIdByServe(loginRes.code);
					if (callBack) {
						callBack(loginRes.code)
					}
				},
				fail: function(loginFail) {
					console.log("loginFail", loginFail);
				}
			});
		},
		fail: res => {
			console.log('getProvider fail', res);
		}
	})
}


/**
 * 将用户跳转到微信公共平台获取用户信息
 */
const gotoH5WxPlatformLoginUrl = () => {
	let url = 'https://h5.sx2b2c.com/wx_shouquan/#/?targetUrl=' + encodeURIComponent(
		window.location.href);
	console.log('gotoH5WxPlatformLoginUrl', url)

	window.location.href = url;
}

const setParam = (param) => {
	uni.setStorageSync('param', param)
}

const getParam = () => {
	return uni.getStorageSync('param');
}

const autoGotoSx2b2cCom = () => {
	if (deviceUtil.isWeChatBrowser()) {
		if (window.location.href.indexOf('localhost') == -1 && window.location.href.indexOf('sx2b2c.com') == -1) {
			let url = 'https://h5.sx2b2c.com' + window.location.pathname;
			if (window.location.search) {
				url += '?' + window.location.search
			}
			if (window.location.hash) {
				url += window.location.hash
			}
			window.location.href = url
			return true
		}
	} else {
		if (window.location.href.indexOf('localhost') == -1 &&
			window.location.href.indexOf('4008825010.cn') == -1) {
			let url = 'http://h5.4008825010.cn' + window.location.pathname;
			if (window.location.search) {
				url += '?' + window.location.search
			}
			if (window.location.hash) {
				url += window.location.hash
			}
			window.location.href = url
			return true
		}
	}
	return false
}

export default {
	setMobile,
	getMobile,
	removeMobile,
	login,
	getToken,
	initUserInfo,
	getUserInfo,
	receive,
	receiveRecordList,
	setUserId,
	getUserId,
	getMiniLoginCode,
	initUserId,
	gotoH5WxPlatformLoginUrl,
	setParam,
	getParam,
	autoGotoSx2b2cCom,
}